package com.house.health.controller;

import com.house.health.domain.*;
import com.house.health.service.IAdminService;
import com.house.health.service.IHouserService;
import com.house.health.service.IOrderService;
import com.house.health.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author DuanChangSuo
 * @date 2023/2/7 21:43
 * @description 类的描述和介绍
 **/
@Controller
public class AdminController {
    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IHouserService iHouserService;

    /**
     * 首页点击管理员，跳转到管理员登录页
     * @return
     */
    @RequestMapping("/toAdminLogin")
    public String toAdminLogin() {
        return "admin";
    }

    /**
     * 登录校验
     * @param username
     * @param userpwd
     * @param req
     * @return
     */
    @RequestMapping("/adminAccess")
    @ResponseBody
    public String adminAccess(String username, String userpwd, HttpServletRequest req) throws Exception {
        //密码加密进行比较
        userpwd = PasswordUtils.encryptBASE64(userpwd.getBytes());
        Admin admin = new Admin(username, userpwd);
        Admin adminAccess = iAdminService.adminAccess(admin);
        req.getSession().setAttribute("Admin", adminAccess);
        if (adminAccess != null)
            return "OK";
        return "FAIL";
    }

    /**
     * 点击登录，调转到管理员后台管理系统
     * @return
     */
    @RequestMapping("/toAdminHomePage")
    public String toAdminHomePage() {
        return "adminhome";
    }

    /**
     * 点击查看所有用户，调转页面
     * @return
     */
    @RequestMapping("/toAllUserPage")
    public String toAllUserPage() {
        return "alluser";
    }

    /**
     * 显示所有注册用户
     * @return
     */
    @RequestMapping("/AllUsers")
    @ResponseBody
    public UserData findAllUser() {
        List<Users> findAllUser = iAdminService.findAllUser();
        UserData u = new UserData();
        u.setCode(0);
        u.setCount(findAllUser.size());
        u.setData(findAllUser);
        u.setMsg("OK");
        return u;
    }

    /**
     * 点击所有注册用户列表最后的修改，（传入id）跳转到修改用户界面
     * @return
     */
    @RequestMapping("/toEditUserPage")
    public String toEditUserPage(int uID, HttpServletRequest req) {
        Users findUserById = iAdminService.findUserById(uID);
        req.getSession().setAttribute("User", findUserById);
        return "editUser";
    }

    /**
     * 更新用户信息，点击提交，调转到所有注册用户界面
     * @param users
     * @return
     */
    @RequestMapping("/editUser")
    @ResponseBody
    public String editUser(Users users) throws Exception {
        /**
         * 管理员修改密码此处也应该对密码进行加密处理
         */
        String userNewPwd = PasswordUtils.encryptBASE64(users.getuPassword().getBytes());
        users.setuPassword(userNewPwd);
        int n = iAdminService.updateUser(users);
        if (n > 0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 管理员删除用户
     *
     * @param uID
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(int uID) {
        int n = iAdminService.deleteUser(uID);
        if (n > 0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 点击用户订单信息，跳转页面
     * @return
     */
    @RequestMapping("/toAllorder")
    public String toAllOrderPage() {
        return "allorder";
    }

    /**
     * 显示所有订单信息
     * @return
     */
    @RequestMapping("/allOrderInfo")
    @ResponseBody
    public UserOrderData findAllOrder(int page, int limit, HttpServletRequest request){
        Page p = new Page();
        p.setPage((page - 1) * limit);
        p.setLimit(limit);
        UserOrderData uod = new UserOrderData();
        List<UserOrder> order = iOrderService.findAllOrder(p);
        uod.setCode(0);
        uod.setCount(order.size());
        uod.setData(order);
        uod.setMsg("200");
        return  uod;
    }

    /**
     * 点击所有订单列表最后的删除，删除订单
     * @return
     */
    @RequestMapping("/deleteOrders")
    @ResponseBody
    public String deleteOrder(int oID) {
        int n = iOrderService.deleteOrder(oID);
        if(n>0)
            return "OK";
        return "FAIL";
    }

    /**
     * 点击管理员密码更改，跳转页面
     * @return
     */
    @RequestMapping("/toUpdateAdminPwdPage")
    public String toUpdateAdminPwdPage() {
        return "updateAdminPwd";
    }


    /**
     * 修改密码，点击提交
     * @param request
     * @param oldpwd
     * @param newpwd
     * @param newpwdagain
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateAdminPwd")
    @ResponseBody
    public String updateAdminPwd(HttpServletRequest request,String oldpwd, String newpwd, String newpwdagain) throws Exception {
        Admin admin = new Admin();
        Admin adminSession = (Admin) request.getSession().getAttribute("Admin");
        admin.setId(adminSession.getId());
        oldpwd = PasswordUtils.encryptBASE64(oldpwd.getBytes());
        admin.setUserpwd(oldpwd);
        Admin checkAdminPwd = iAdminService.checkAdminPwd(admin);
        if (checkAdminPwd == null) {
            return "ERROR";
        }
        if (!newpwd.equals(newpwdagain)) {
            return "FAIL";
        }
        Admin a = new Admin();
        a.setId(adminSession.getId());
        newpwd = PasswordUtils.encryptBASE64(newpwd.getBytes());
        a.setUserpwd(newpwd);
        int n = iAdminService.updateAdminPwd(a);
        if (n > 0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 点击所有房源信息，跳转页面
     * @return
     */
    @RequestMapping("/toAllHousePage")
    public String toAllHousePage() {
        return "allhouse";
    }

    /**
     * 查询所有房源
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findAllHouse")
    @ResponseBody
    public UserHouseData findAllHouse(int page, int limit) {
        Page p = new Page();
        p.setLimit(limit);
        p.setPage((page - 1) * limit);
        List<House> findAllHouse = iAdminService.findAllHouse(p);
        UserHouseData data = new UserHouseData();
        data.setCode(0);
        data.setCount(findAllHouse.size());
        data.setData(findAllHouse);
        data.setMsg("OK");
        return data;
    }

    /**
     * 跳转到管理员更新房源界面
     * @param hID
     * @param request
     * @return
     */
    @RequestMapping("/toAdminUpdateHousePage")
    public String toUpdatePage(int hID, HttpServletRequest request) {
        House house = iHouserService.findHouseDetailsById(hID);
        request.getSession().setAttribute("House", house);
        return "updatehouse";
    }

//    修改房源信息，在HouseController中的updateHouse
//    点击立即修改，跳转到所有房源信息列表界面

    /**
     * 管理员删除房源
     * @param hID
     * @return
     */
    @RequestMapping("/deleteHouse")
    @ResponseBody
    public String deleteHouse(int hID) {
        int deleteHouse = iAdminService.deleteHouse(hID);
        if (deleteHouse > 0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     *管理员退出，调转到登录界面
     *
     * @param request
     * @return
     */
    @RequestMapping("/adminSingnout")
    public String signout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "admin";
    }

    @RequestMapping("/welcome01")
    public String toWelcomePage() {
        return "welcome01";
    }
}